<script lang="ts" setup>
import { ref } from 'vue'
import { GetBrandofficialFenrunDetail } from '@/api/modules/brandofficial'
import GetCacheData from '@/store/modules/NewPageCache'

const route = useRoute()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('brandofficialFenrunDetail').currentPage

const findFrom = reactive({
  FenRunId: route.query.id,
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.FenRunId && ({ FenRunId: findFrom.FenRunId })),
  }
  await GetBrandofficialFenrunDetail(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('brandofficialFenrunDetail').currentSize)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('brandofficialFenrunDetail', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('brandofficialFenrunDetail').currentSize)
  cacheData.SetCacheData('brandofficialFenrunDetail', val, cacheData.GetCacheData('brandofficialFenrunDetail').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('brandofficialFenrunDetail').currentPage || 1
})

onMounted(() => {
  GetDataList(cacheData.GetCacheData('brandofficialFenrunDetail').currentPage, cacheData.GetCacheData('brandofficialFenrunDetail').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="分润Id">
                <el-input v-model="(findFrom.FenRunId as string)" placeholder="请输入分润Id" disabled @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="fenrunId" label="分润ID" width="100" />
        <el-table-column align="center" prop="orderDetailId" label="订单明细ID" width="150" />
        <el-table-column align="center" prop="productId" label="购买商品ID" width="150" />
        <el-table-column align="center" prop="productNums" label="商品购买数量" width="150" />
        <el-table-column align="center" prop="priceGap" label="价差" width="150" />
        <el-table-column align="center" prop="fenrunAmount" label="商品分润金额" width="150" />
        <el-table-column align="center" prop="desp" label="描述" min-width="220" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="220" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="220" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('brandofficialFenrunDetail').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
